/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: worker_discovery_proxy.hpp
 */

#ifndef _WORKER_DISCOVERY_PROXY_HPP_
#define _WORKER_DISCOVERY_PROXY_HPP_

#include <iostream>
#include <string>

#include "proxy_exception.hpp"
#include "discovery.hpp"
#include "worker_discovery.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
/*! \struct WorkerDiscProxyConfig
 * \brief Configuration for WorkerDiscoveryProxy.
 *
 * Configuration structure for WorkerDiscoveryProxy.
 */
typedef struct {
	/*!
	 * Represents the Ip v4 address to connect to WorkerDiscovery of another
	 * peer in order to let a Worker notify a WorkerDiscovery that its
	 * status has changed.
	 */
	std::string ip_to_disc; /* IP address to discovery */
	/*!
	 * Represents the TCP Port to connect to WorkerDiscovery of another
	 * peer in order to let a Worker notify a WorkerDiscovery that its
	 * status has changed.
	 */
	std::string port_to_disc; /* Port to discovery */
	/*!
	 * Represents the pointer to WorkerDiscovery in order to let a Manager
	 * ask for the most suitable Worker for a TaskDescriptor of a task to
	 * execute.
	 */
	WorkerDiscovery* ptrto_worker;
} WorkerDiscProxyConfig;
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*! \class WorkerDiscoveryProxy
 * \brief Interface to communicate to WorkerDiscovery.
 *
 * This class provides network and local interface to communicate to
 * WorkerDiscovery. It is important to notice that constructor is private in
 * order to let a factory perform instantiation.
 */
class WorkerDiscoveryProxy : public Discovery {
	/*!
	 * To let factory build properly this object, we provide access to every
	 * part of it.
	 */
	friend class DiscoveryProxyFactory;
public:
	/*!
	 * Destructor.
	 */
	~WorkerDiscoveryProxy();
	/*!
	 * Used to query a manager for a Worker to use to execute a task.
	 * \param taskdesc TaskDescriptor provided to get the Address of
	 * the Worker that best fits the execution of that task.
	 * \return Returns the Address of the Worker chosen by the WorkerDiscovery.
	 * \exception ProxyException Thrown when WorkerDiscovery encouters a problem
	 * in obtaining information about all known Workers by the peer.
	 */
	Address get_worker(const TaskDescriptor& taskdesc) throw(ProxyException);
	/*!
	 * This function is generally used by a Worker to notify a WorkerDiscovery
	 * that its status has changed. Thanks to this method, the Worker can send
	 * its new status inside its descriptor.
	 * \param workerdesc WorkerDescriptor to send containing new information
	 * and staatus of the Worker.
	 * \exception ProxyException Thrown when a network problem occurs during
	 * sending.
	 */
	void update_worker(const WorkerDescriptor& workerdesc)
			throw(ProxyException);
private:
	/*!
	 * Privately constructs the object. Default configuration with loopback
	 * address and invalid port.
	 */
	WorkerDiscoveryProxy();
	/*!
	 * Privately constructs the object. Assigning configuration.
	 */
	explicit WorkerDiscoveryProxy(WorkerDiscProxyConfig conf);
	/*!
	 * Parameters for proxy.
	 */
	WorkerDiscProxyConfig _config;
}; /* WorkerDiscoveryProxy */
} /* middleware */

#endif
